#include <REGX52.H>
#include "INTRINS.H"
#include "HT1632C.h"

sbit HT1632C_RD=P0^0;
sbit HT1632C_DATA=P0^1;
sbit HT1632C_WR=P0^2;
sbit HT1632C_CS=P0^3;

unsigned char HT1632C_ReadPageData[32];

unsigned char HT1632C_ReadSingleData(unsigned char Address)
{
    unsigned char i,Data=0;
    Address&=0x7F;
    HT1632C_CS=0;
    for(i=0;i<3;i++)
    {
        HT1632C_DATA=HT1632C_Read&(0x04>>i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    for(i=0;i<7;i++)
    {
        HT1632C_DATA = Address & (0x40 >> i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    HT1632C_DATA=1;
    for(i=0;i<4;i++)
    {
        HT1632C_RD = 0;
        _nop_();
        _nop_();

        
        if(HT1632C_DATA)
        {
            Data=Data|(0x01<<i);

        }
        HT1632C_RD=1;
        _nop_();
        _nop_();

    }
    HT1632C_CS = 1;
    return Data;
}
void HT1632C_ReadData(unsigned char Address)
{
    unsigned char i,j, Data = 0;
    Address &= 0x7F;
    HT1632C_CS = 0;
    for (i = 0; i < 3; i++)
    {
        HT1632C_DATA = HT1632C_Read & (0x04 >> i);
        HT1632C_WR = 0;
        HT1632C_WR = 1;
    }
    for (i = 0; i < 7; i++)
    {
        HT1632C_DATA = Address & (0x40 >> i);
        HT1632C_WR = 0;
        HT1632C_WR = 1;
    }
    HT1632C_DATA = 1;
    for (i = 0; i < (0x3F-Address); i++)
    {
        for(j=0;j<8;j++)
        {
            HT1632C_RD = 0;
            HT1632C_RD = 1;
            if (HT1632C_DATA)
            {
                Data = Data | (0x01 << i);
            }
        }
        HT1632C_ReadPageData[0x3F-Address]=Data;
        Data=0;
    }
    HT1632C_CS=1;
}

void HT1632C_WriteSingleData(unsigned char Address,unsigned char Data)
{
    unsigned char i;
    HT1632C_CS=0;
    Address &= 0x7F;
    for (i = 0; i < 3; i++)
    {
        HT1632C_DATA = HT1632C_Write & (0x04 >> i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    for (i = 0; i < 7; i++)
    {
        HT1632C_DATA = Address & (0x40 >> i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    for(i=0;i<4;i++)
    {
        HT1632C_DATA=Data&(0x01<<i);
        HT1632C_WR=0;
        _nop_();
        _nop_();
        HT1632C_WR=1;
        _nop_();
        _nop_();
    }
    HT1632C_CS=1;
}

void HT1632C_WriteData(unsigned char Address, unsigned char *Data)
{
    unsigned char i,j;
    HT1632C_CS = 0;
    Address &= 0x7F;
    for (i = 0; i < 3; i++)
    {
        HT1632C_DATA = HT1632C_Write & (0x04 >> i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    for (i = 0; i < 7; i++)
    {
        HT1632C_DATA = Address & (0x40 >> i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    for (i = 0; i < (0x3F - Address); i++)
    {
        for(j=0;j<8;j++)
        {
            HT1632C_DATA = Data[i] & (0x01 << i);
            HT1632C_WR = 0;
            _nop_();
            _nop_();
            HT1632C_WR = 1;
            _nop_();
            _nop_();
        }

    }
    HT1632C_CS = 1;
}

void HT1632C_WriteCommand(unsigned char Command)
{
    unsigned char i;
    HT1632C_CS=0;
    for (i = 0; i < 3; i++)
    {
        HT1632C_DATA = HT1632C_Command & (0x04 >> i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR = 1;
        _nop_();
        _nop_();
    }
    for(i=0;i<8;i++)
    {

        HT1632C_DATA=Command & (0x80>>i);
        HT1632C_WR = 0;
        _nop_();
        _nop_();
        HT1632C_WR=1;
        _nop_();
        _nop_();
    }
    HT1632C_CS=1;
}

void HT1632C_Init()
{
    HT1632C_WriteCommand(HT1632C_SYS_DIS);
    HT1632C_WriteCommand(HT1632C_COM_Option);
    HT1632C_WriteCommand(HT1632C_EXT_CLK_Master_Mode);
    HT1632C_WriteCommand(HT1632C_Slave_Mode);
    HT1632C_WriteCommand(HT1632C_SYS_EN);
    HT1632C_WriteCommand(HT1632C_LED_ON);
		HT1632C_DATA=1;
}